package com.example.mykotlindemo01.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.mykotlindemo01.R
import kotlinx.android.synthetic.main.activity_demo311.*
import java.util.*
import kotlin.concurrent.timerTask

class Demo311 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_demo311)
        initView()
        initData()
        initEvent()

    }

    private fun initView() {

        tv_view.setText("倒计时长 $i")

    }

    private fun initEvent() {
        btn_on.setOnClickListener {
            getTime()
        }

    }

    private fun initData() {



    }

    private val timer1: Timer
        get() {
            val timer = Timer()
            return timer
        }


    private val i: Int
        get() {
            var sum = 30
            return sum
        }

    private fun getTime() {
        var sum = i
        val timer = timer1
        timer.schedule(timerTask {

            runOnUiThread {

                if (sum > 0) {
                    sum--
                    tv_view.setText("剩余秒数："+ sum);
                }else{timer.cancel()}
            }
//            Log.e("TAG", "initView: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" )

        },1000,1000)

    }


}